<?php

declare(strict_types=1);

namespace App\Controller\Api;

use App\Controller\AbstractController;
use App\Utils\ApiUtils;
use Hyperf\Utils\Context;
use Hyperf\Di\Annotation\Inject;
use App\Service\CouponService;
use App\Validate\CouponValidation;
use App\Constants\ErrorCode;

class CouponController extends AbstractController
{

    /**
     * @Inject
     * @var CouponService
     */
    protected $service;

    /**
     * 领取优惠券
     * @Author czm
     * @DateTime 2020-04-30
     * @return void
     */
    public function receive()
    {
        make(CouponValidation::class)->check($this->request->all(), __FUNCTION__)->response();
        $coupon_id = (int) $this->request->input('coupon_id', '');
        $openid = $this->request->input('openid', '');
        $jwt = Context::get('jwt_token')['account'];
        $this->service->receive(compact('coupon_id', 'openid', 'jwt'));
        return $this->response->json(ApiUtils::send(ErrorCode::SUCCESS, '领取成功'));
    }
}
